package com.yunlan.materia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunlan.materia.entity.DoctorInfo;
import com.yunlan.materia.enums.BaseStatusEnum;
import com.yunlan.materia.enums.BusinessEnum;
import com.yunlan.materia.enums.DeleteEnum;
import com.yunlan.materia.exceptions.GlobalException;
import com.yunlan.materia.mapper.DoctorInfoMapper;
import com.yunlan.materia.request.DeleteDisableDoctorRequest;
import com.yunlan.materia.request.DoctorDetailsRequest;
import com.yunlan.materia.request.DoctorInfoRequest;
import com.yunlan.materia.response.DoctorInfoResponse;
import com.yunlan.materia.service.DoctorInfoService;
import com.yunlan.materia.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 罗显
 * @date 2025/9/18
 * @ApiNote
 */
@Slf4j
@Service
public class DoctorInfoServiceImpl implements DoctorInfoService {

    @Resource
    DoctorInfoMapper doctorInfoMapper;


    @Override
    public DoctorInfoResponse createDoctorDoc(DoctorInfoRequest request) {
        DoctorInfo doctorInfo = queryMobile(request.getContactPhone());
        if (doctorInfo != null) {
            throw new GlobalException(BusinessEnum.DATA_EXIST.getCode(),BusinessEnum.DATA_EXIST.getMsg());
        }
        DoctorInfo entity = new DoctorInfo();
        BeanCopier beanCopier = BeanCopier.create(DoctorInfoRequest.class, DoctorInfo.class,false);
        beanCopier.copy(request,entity,null);

        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        entity.setOperatingTime(now);
        entity.setDelete(DeleteEnum.NORMAL.getCode());
        entity.setDoctorStatus(BaseStatusEnum.NORMAL.getCode());
        entity.setUniqueCode(StringUtils.generatorUniqueCodeByType("doctor"));
        doctorInfoMapper.insert(entity);

        DoctorInfo queryMobile = queryMobile(request.getContactPhone());

        DoctorInfoResponse response = new DoctorInfoResponse();
        BeanCopier beanCopier2 = BeanCopier.create(DoctorInfo.class, DoctorInfoResponse.class,false);
        beanCopier2.copy(queryMobile,response,null);
        return response;
    }

    @Override
    public Map<String, Object> updateDoctor(DoctorDetailsRequest request) {
        return Collections.emptyMap();
    }

    @Override
    public DoctorInfoResponse deleteDisableDoctor(DeleteDisableDoctorRequest request) {
        return null;
    }

    @Override
    public List<DoctorInfo> queryLikeDoctor(DoctorDetailsRequest request) {
        return Collections.emptyList();
    }

    @Override
    public Map<String, Object> queryDoctor(DoctorDetailsRequest request) {
        return Collections.emptyMap();
    }

    @Override
    public DoctorInfo queryById(Long id) {
        return doctorInfoMapper.selectById(id);
    }

    @Override
    public DoctorInfo queryMobile(String mobile) {
        return doctorInfoMapper.selectOne(new LambdaQueryWrapper<DoctorInfo>().eq(DoctorInfo::getContactPhone,mobile));
    }

    @Override
    public List<DoctorInfoResponse> selectList() {
        List<DoctorInfo> doctorInfos = doctorInfoMapper.selectList(null);
        if (CollectionUtils.isEmpty(doctorInfos)){
            return Collections.emptyList();
        }
        List<DoctorInfoResponse> responses = new ArrayList<>();
        for (DoctorInfo doctorInfo : doctorInfos) {
            DoctorInfoResponse response = new DoctorInfoResponse();
            BeanCopier beanCopier = BeanCopier.create(DoctorInfo.class, DoctorInfoResponse.class,false);
            beanCopier.copy(doctorInfo,response,null);
            responses.add(response);
        }
        return responses;
    }


}
